package com.pxmeta.app.robot;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pxmeta.common.constant.UserVisitMode;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.UserVisit;
import com.pxmeta.user.service.IUserFollowService;
import com.pxmeta.user.service.IUserService;
import com.pxmeta.user.service.IUserVisitService;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shoushen.luan
 * @since 2022-10-19
 */
@Service
@Slf4j
@EnableScheduling
public class RobotService {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserFollowService userFollowService;
    @Autowired
    private IUserVisitService visitService;




    /**
     * 关注用户
     *
     * @param robotUserId
     * @param targetUserId
     * @param circleId
     */
    private void followUser(Long robotUserId, Long targetUserId, Long circleId) {
        userFollowService.followUser(robotUserId, targetUserId, circleId);
    }

    /**
     * 创建访问主页记录
     *
     * @param robotUserId
     * @param targetUserId
     */
    private void createVisitHome(Long robotUserId, Long targetUserId) {
        UserVisit userVisit = UserVisit.builder()
                .userId(robotUserId)
                .targetUserId(targetUserId)
                .visitMode(UserVisitMode.HOME)
                .visitDate(LocalDateTime.now()).build();
        visitService.save(userVisit);
    }

    private void process(Date beginTime, Date endTime, Consumer<User> consumer) {
        IPage<User> page = new Page<>();
        page.setSize(10);
        page.setCurrent(1);
        log.info("robot process begin:{},endTime:{}", beginTime, endTime);
        for (; ; ) {
            page = userService.getUserList(page, beginTime, endTime);
            page.getRecords().forEach(consumer);
            if (page.getRecords().size() >= page.getSize()) {
                page.setCurrent(page.getCurrent() + 1);
            } else {
                break;
            }
        }
    }
}
